//
//  AllCategroyView.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/7/29.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "AllCategroyView.h"

#import "AllCategroySectionView.h"//SectionHeaderView

#import "AllCategroyTableViewCell.h"//全部Cell
#import "AllCategroyPartTableViewCell.h"//一行Cell


@implementation AllCategroyView
{
    NSMutableArray *_categroyArray;//分类数组
    NSMutableArray *_foldArray;//折叠数组
    
    NSMutableArray *_selectArray;//存放选择规格的数组
    
    NSMutableArray *_cellHeightArray;//cell高数组
    
}

#define viewWidth SCREEN_WIDTH - 50

 static NSString *const AllCategroyTableViewCellID = @"AllCategroyTableViewCell";
 static NSString *const AllCategroyPartTableViewCellID = @"AllCategroyPartTableViewCell";

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setView];
    }
    return self;
}

- (void)setView{
    //设置页眉页脚
    [self setHeaderFooterView];
    
    //初始化数据源
    [self createData];
    
    //创建TableView
    [self createTableView];
    
    //注册tableView
//    [self registerTableView];
}
#pragma mark === 设置页眉页脚 ===
- (void)setHeaderFooterView{
    UILabel *all = [[UILabel alloc] initWithFrame:CGRectMake(10, 32, 64, 20)];
    all.text = @"全部分类";
    all.font = [UIFont systemFontOfSize:16];
    [self addSubview:all];
    
    UIButton *allbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    allbtn.frame = CGRectMake(viewWidth - 80, 27, 80, 30);
    [allbtn addTarget:self action:@selector(allCategroy) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:allbtn];
    
    UILabel *allLabel = [[UILabel alloc] initWithFrame:CGRectMake(viewWidth - 70, 27, 30, 30)];
    allLabel.text = @"全部";
    allLabel.textColor = [UIColor lightGrayColor];
    allLabel.font = [UIFont systemFontOfSize:15];
    [self addSubview:allLabel];
    
    UIImageView *allImage = [[UIImageView alloc] initWithFrame:CGRectMake(viewWidth - 35, 32, 10, 20)];
    allImage.image = [UIImage imageNamed:@"DDJSicon (1)"];
    [self addSubview:allImage];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 63, viewWidth, 1)];
    line.backgroundColor = BackgroundCOLOR;
    [self addSubview:line];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 49, viewWidth, 1)];
    line1.backgroundColor = BackgroundCOLOR;
    [self addSubview:line1];
    
    UIButton *resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    resetBtn.frame = CGRectMake(0, SCREEN_HEIGHT - 48, line.frame.size.width / 2, 48);
    [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    [resetBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    resetBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [resetBtn addTarget:self action:@selector(reset) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:resetBtn];
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    doneBtn.frame = CGRectMake(resetBtn.frame.size.width, SCREEN_HEIGHT - 48, line.frame.size.width / 2, 48);
    [doneBtn setTitle:@"确定" forState:UIControlStateNormal];
    doneBtn.backgroundColor = APPCOLOR;
    [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    doneBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [doneBtn addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:doneBtn];
}

- (void)createData{
    NSArray *array = @[
                       @{@"品牌"    :  @[@"都市迷人", @"古令 (GULING)", @"曼当芬 (MAN)", @"都市迷人", @"古令 (GULING)", @"曼当芬 (MAN)"]},
                       @{@"胸围尺码" : @[@"70A", @"75A", @"80A", @"70B", @"75B", @"80B", @"70C", @"75C", @"80C"]},
                       @{@"款式"    :  @[@"平角裤", @"套装", @"三角裤", @"秋裤", @"打底裤"]},
                       @{@"有无钢圈" :  @[@"无钢圈", @"有钢圈"]},
                       @{@"适合季节" :  @[@"夏季", @"春季", @"秋季", @"冬季", @"春秋季"]},
                       @{@"适合胸型" :  @[@"小胸MM", @"大胸MM", @"下垂"]},
                       @{@"人群"    :  @[@"儿童", @"成人", @"中老年"]}
                       ];
    _categroyArray = [NSMutableArray array];
    [_categroyArray addObjectsFromArray:array];
    
    _foldArray = [NSMutableArray array];
    _selectArray = [NSMutableArray array];
    _cellHeightArray = [NSMutableArray array];
    for (int i = 0; i < _categroyArray.count; i++) {
        [_foldArray addObject:[NSNumber numberWithBool:NO]];
        [_selectArray addObject:@[@"全部",@"-1"]];
        [_cellHeightArray addObject:@""];
    }
}
#pragma mark === 创建TableView ===
- (void)createTableView{
    _tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,64, viewWidth, SCREEN_HEIGHT - 64 - 51)style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
    [self addSubview:_tableView];
    
    [self.tableView registerClass:[AllCategroyTableViewCell class] forCellReuseIdentifier:AllCategroyTableViewCellID];
    [self.tableView registerClass:[AllCategroyPartTableViewCell class] forCellReuseIdentifier:AllCategroyPartTableViewCellID];
}


#pragma mark === TableViewDelegate ===
//section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _categroyArray.count;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([_foldArray[indexPath.section] boolValue]) {
        NSDictionary *dic = _categroyArray[indexPath.section];
        int line = (int)[dic.allValues[0] count] / 3;
        if ([dic.allValues[0] count] % 3 == 0) {
            return 50 * line;
        }else{
            return 50 * (line + 1);
        }
    }
    return 50;
}
//sectionView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    AllCategroySectionView *headerView = [[AllCategroySectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 40, 40)];
    headerView.backgroundColor = [UIColor whiteColor];
    NSDictionary *dic = _categroyArray[section];
    headerView.categroyName = dic.allKeys[0];
    headerView.selectString = [NSString stringWithFormat:@"%@",_selectArray[section][0]] ;
    headerView.isFolding = [_foldArray[section] boolValue];
    __weak __typeof__(AllCategroySectionView *) weakHeaderView = headerView;
    headerView.fold = ^(){
        BOOL bo = [_foldArray[section] boolValue];
        [_foldArray replaceObjectAtIndex:section withObject:[NSNumber numberWithBool:!bo]];
        CGFloat scroll = _tableView.contentOffset.y;
        [_tableView removeFromSuperview];
        [self createTableView];
        _tableView.contentOffset = CGPointMake(0, scroll);
        weakHeaderView.foldImage.transform = CGAffineTransformMakeRotation(M_PI);
    };
    [headerView setNeedsLayout];
    return headerView;
}
//section高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
//Reusable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([_foldArray[indexPath.section] boolValue]) {
        AllCategroyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AllCategroyTableViewCellID];
        if (!cell) {
            cell = [[AllCategroyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AllCategroyTableViewCellID];
        }
        NSDictionary *dic = _categroyArray[indexPath.section];
        cell.array = dic.allValues[0];
        NSLog(@"%@", cell.array);
        cell.selectNum = _selectArray[indexPath.section][1];
        [cell setCell];
        cell.select = ^(NSString *string, NSString *temp){
            [_selectArray replaceObjectAtIndex:indexPath.section withObject:@[string, temp]];
            [_tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
        };
        
        cell.selectionStyle = UITableViewCellStyleDefault;
        return cell;
    }
    
    AllCategroyPartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AllCategroyPartTableViewCellID];
    if (!cell) {
        cell = [[AllCategroyPartTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AllCategroyPartTableViewCellID];
    }
    NSDictionary *dic = _categroyArray[indexPath.section];
    NSArray *arr;
    if ([dic.allValues[0] count] < 3) {
        arr = dic.allValues[0];
    }else{
        arr = @[dic.allValues[0][0], dic.allValues[0][1], dic.allValues[0][2]];
    }
    cell.array = arr;
    NSLog(@"%@", cell.array);
    cell.selectNum = _selectArray[indexPath.section][1];
    [cell setCell];
    cell.select = ^(NSString *string, NSString *temp){
        [_selectArray replaceObjectAtIndex:indexPath.section withObject:@[string, temp]];
        [_tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
    };
    
    cell.selectionStyle = UITableViewCellStyleDefault;
    return cell;
}

/**全部分类*/
- (void)allCategroy{
    NSLog(@"全部分类");
}
/**重置*/
- (void)reset{
    [_selectArray removeAllObjects];
    for (int i = 0; i < _categroyArray.count; i++) {
        [_selectArray addObject:@[@"全部",@"-1"]];
    }
    [_tableView reloadData];
}
/**确定*/
- (void)done{
    NSLog(@"确定");
}

- (UITableView *)tableView{
    if (!_tableView) {
        
    }
    return _tableView;
}

@end
